package com.drinks.yiniang.adapter.community;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.banner.CommentsBannerAdapter;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.adapter.photo.PreviewPhotosAdapter;
import com.drinks.yiniang.entity.comment.MyCommentsJson;
import com.drinks.yiniang.entity.community.CommunityNewsJson;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.recycler.PreviewRecyclerView;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区活动列表adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class UCCommentsAdapter extends BaseQuickRCVAdapter<MyCommentsJson.MyCommentsList, BaseViewHolder> {

    private AppCompatTextView mTextView;

    private Drawable mCollectNormalDrawable;
    private Drawable mCollectFocusDrawable;
    private Drawable mLikeNormalDrawable;
    private Drawable mLikeFocusDrawable;

    private PagerSnapHelper mSnapHelper;
    private LinearLayoutManager mLayoutManager;
    private CommentsBannerAdapter mBannerAdapter;

    private SpannableStringBuilder mBuilder;

    private int end;

    public UCCommentsAdapter(Context mContext, @Nullable List<MyCommentsJson.MyCommentsList> data) {
        super(R.layout.item_uc_comments, data);

        mCollectNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_n);
        mCollectFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_wishlist_p);

        mLikeNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_n);
        mLikeFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_like_p);

        mBuilder = new SpannableStringBuilder();
    }

    @Override
    protected void convert(BaseViewHolder helper, MyCommentsJson.MyCommentsList item) {
        if (item.getFans() == null) {
            item.setFans(new MyCommentsJson.MyCommentsUser());
        }

        //头像
        GlideUtils.loadAvatar(mContext, (CircleImageView) helper.getView(R.id.iv_uc_comments_avatar), item.getFans().getAvatar());
        //昵称
        helper.setText(R.id.tv_uc_comments_nickname, item.getFans().getUsername());
        //banner
        setBanners((PreviewRecyclerView) helper.getView(R.id.rcv_uc_comments_banner), item.getImages());
        //关联的商品名称
        helper.setText(R.id.tv_uc_comments_goods_name, item.getGoods_name());
        //商品推荐语
        helper.setText(R.id.tv_uc_comments_goods_slogan, item.getSub_title());
        //商品价格
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append("¥");
        mBuilder.append(StringUtils.getNullEmptyConvert__(item.getPrice()));
        helper.setText(R.id.tv_uc_comments_goods_price, mBuilder);
        //当前人的评论
        if (item.getThis_comment() == null) {
            item.setThis_comment(new MyCommentsJson.MyCommentsInfo());
        }
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append("“");
        mBuilder.append(StringUtils.getNoNullString(item.getThis_comment().getContent()));
        mBuilder.append("”");
        helper.setText(R.id.tv_uc_comments_title, mBuilder);
        //收藏点赞
        setCollectAndLikeData((TextView) helper.getView(R.id.tv_uc_comments_collect), (TextView) helper.getView(R.id.tv_uc_comments_like), item);
        //总评论数
        helper.setText(R.id.tv_uc_comments_total_comments, String.format(mContext.getString(R.string.text_total_comment_format), StringUtils.getNullEmptyConvert__(item.getComment_count())));
        //评论列表
        setCommentList((LinearLayout) helper.getView(R.id.ll_uc_comments_comments), item.getComment_list());
        //时间
        helper.setText(R.id.tv_uc_comments_time, "这里是时间");

        helper.addOnClickListener(R.id.iv_uc_comments_three_dot);
        helper.addOnClickListener(R.id.tv_uc_comments_collect, R.id.tv_uc_comments_like, R.id.tv_uc_comments_total_comments, R.id.tv_uc_comments_comments_add);
    }

    //设置图片banner
    private void setBanners(PreviewRecyclerView rcv, List<CommunityNewsJson.CommunityNewsImg> images) {
        mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        rcv.setLayoutManager(mLayoutManager);

        mBannerAdapter = new CommentsBannerAdapter(mContext, images);
        mBannerAdapter.bindToRecyclerView(rcv);
        mSnapHelper = new PagerSnapHelper();
        rcv.setOnFlingListener(null);
        mSnapHelper.attachToRecyclerView(rcv);
    }

    private void setCollectAndLikeData(TextView tvCollect, TextView tvLike, MyCommentsJson.MyCommentsList item) {
        //收藏数
        tvCollect.setText(StringUtils.getNullEmptyConvertZero(item.getCollect_count()));
        tvCollect.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_collect()) ? mCollectFocusDrawable : mCollectNormalDrawable, null, null, null);
        //点赞数
        tvLike.setText(StringUtils.getNullEmptyConvertZero(item.getLike_count()));
        tvLike.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_like()) ? mLikeFocusDrawable : mLikeNormalDrawable, null, null, null);
    }

    //设置评论列表数据
    private void setCommentList(LinearLayout ll, List<MyCommentsJson.MyCommentsInfo> list) {
        ll.removeAllViews();

        if (CommonUtils.isEmptyList(list)) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            mBuilder.clearSpans();
            mBuilder.clear();
            mBuilder.append(StringUtils.getNoNullString(list.get(i).getNickname()));
            end = mBuilder.length();
            mBuilder.append(" ");
            mBuilder.append(StringUtils.getNoNullString(list.get(i).getContent()));
            mBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


            mTextView = new AppCompatTextView(mContext);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            mTextView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_282829));
            mTextView.setLines(1);
            mTextView.setEllipsize(TextUtils.TruncateAt.END);
            mTextView.setText(mBuilder);
        }
    }
}
